<?php

require "database.php";

system("rm -rf screen/*");

$filename=md5(time().mt_rand(111,999));

$shell="adb shell screencap -p > screen/".$filename.".png";

system($shell);


$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_PORT => "8080",
CURLOPT_URL => "http://127.0.0.1:8080/battery",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 1000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => [
"Accept: */*",
"Accept-Encoding: gzip, deflate, br",
"Connection: keep-alive",
"User-Agent: PostmanRuntime-ApipostRuntime/1.1.0"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  // echo $response;
}
$responses=json_decode($response,1);

$bat=trim($responses["data"]);
$battery=trim(explode(":",$bat)[1]);

$si=trim($responses["size"]);
$size=trim(explode(":",$si)[1]);

$memtot=trim($responses["memtotal"]);
$memtotal=trim(explode(":",$memtot)[1]);
$memtotal=(int)$memtotal;

$cpu=trim($responses["cpuinfo"]);
$cpuinfo=trim(explode(":",$cpu)[1]);

$time=trim($responses["btime"]);
$btime=trim(explode(" ",$time)[1]);
$btime=date("Y-m-d H:i:s",(int)$btime);

$dfh=trim($responses["df"]);
$df=explode(" ",$dfh);
$dfs[0]=$df[16];
$dfs[1]=$df[20];
$dfs[2]=$df[22];

$acti=trim($responses["activity"]);
$activi=explode(" ",$acti)[2];
$activity=explode("/",$activi)[0];

$cpud=trim($responses["cpu"]);
$__cpud__=(int)str_replace('%sys', "", explode(" ",$cpud)[7]);
$__cpud__=$__cpud__>100 ? $__cpud__/10 :$__cpud__;

$mem=trim($responses["mem"]);
// $memd=explode(" ",$mem);
$memd=number_format((((int)str_replace('M', "", explode(" ",$mem)[20]))/((int)str_replace("G","",explode(" ",$mem)[6])*1024))*100,2);



$zb=$_POST["zb"];
$mzbs=$_POST['zbs'];
$data1=$_POST["data1"];
$data2=$_POST["data2"];
$zbs=$zb[count($zb)-1]+3;
if($zbs>60){
if($zbs>=3600){
$zbs=(string)date("H:i",$zbs)."h";
}else{

$zbs=(string)date("i:s",$zbs)."m";
}
}else{
$zbs=(string)ceil($zbs)."s";
}


array_push($zb, $zb[count($zb)-1]+3);
array_shift($zb);

array_push($mzbs,$zbs);
array_shift($mzbs);

array_push($data1, $__cpud__);
array_shift($data1);

array_push($data2, $memd);
array_shift($data2);

$d=[
"path"=>"screen/".$filename.".png",
"battery"=>$battery,
"size"=>$size,
"memtotal"=>(floor($memtotal/1024))."Kb",
"cpuinfo"=>$cpuinfo,
"btime"=>$btime,
"df"=>$dfs,
"activity"=>$activity,
"data1"=>$data1,
"data2"=>$data2,
"zb"=>$zb,
"zbs"=>$mzbs
];
// var_dump($d);

return json($d);

